// index
/**
 * 引用工具
 */
import {
    Ble
} from "../../utils/ble"
// import { Protocol } from "../../utils/protocol"
const cloud = require('../../utils/cloudFunction')
const con = require('../../utils/control')

//引用app.js的全局函数声明
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        clock: '',
        hList: [],
        // rList: [],
        // dList: [],
        bleStatus: '',
        bleInit: false,
        leftDrawer: false,
        disAddBtn: false,
        chooseDeviceList: [{
                id: "01",
                name: "色温灯具",
                context: "适用于冷暖色温调节的灯泡、筒灯、吸顶灯等",
                imgSrc: "/image/icon/device_cct.png",
                type: "CCT"
            },
            {
                id: "02",
                name: "全彩灯具",
                context: "适用于彩色调节的灯泡、筒灯、吸顶灯等",
                imgSrc: "/image/icon/device_rgb.png",
                type: "RGB"
            },
            {
                id: "03",
                name: "单色灯具",
                context: "适用于单色调节的灯泡、筒灯、吸顶灯等",
                imgSrc: "/image/icon/brightness_dim.png",
                type: "PURE"
            },
            {
                id: "04",
                name: "开关",
                context: "通用灯具的常规开关功能",
                imgSrc: "/image/icon/switch.png",
                type: "OLD"
            }
        ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        console.log("index onLoad")
        cloud.firstGetHomeList().then(function name(params) {
            that.setData({
                hList: app.globalData.homeList
            })
            wx.showToast({
                title: '获取房间成功',
                duration: 2000
            })
        })
        // getHomeList
        // cloud.getHomeList().then(function name(params) {
        // })

        // initPresetCache
        setTimeout(() => {
            con.initPresetActiveCache()
        }, 1000);

        //蓝牙初始化并提供蓝牙状态标志
        Ble.initBle({
            success: (res) => {
                console.log(JSON.stringify(res) + "*****")
                this.setData({
                    bleStatus: "初始化蓝牙成功",
                    bleInit: true
                })
            },
            fail: (error) => {
                console.log(JSON.stringify(error) + "----")
                this.setData({
                    bleStatus: "请重启蓝牙后，重新启动小程序"
                })
            }
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // cloud.changeStatus()
        var that = this
        this.data.clock = setInterval(() => {
            // console.log("index clocking !!!")
            if (app.globalData.refreshStatus) {
                that.setData({
                    hList: app.globalData.homeList
                })
                app.globalData.refreshStatus = false
                console.log("index update -------------------------")
            }
        }, 500);

        this.setData({
            hList: app.globalData.homeList
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        console.log("index onHide")
        clearInterval(this.data.clock)
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        console.log("index onUnload")
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    // 显示侧边栏按钮   ==========================================================

    // 显示侧边栏按钮        
    showDrawer() {
        this.setData({
            leftDrawer: true,
        })
    },

    // 关闭侧边栏按钮 
    closeDrawer(e) {
        this.setData({
            leftDrawer: false
        })
    },


    //侧边栏跳转 打开对应页面
    gotoScan() {
        wx.navigateTo({
            url: '/pages/index/sideBar/scan/scan',
        })
        this.closeDrawer()
    },
    gotoQrcode() {
        wx.navigateTo({
            url: '/pages/index/sideBar/qrcode/qrcode',
        })
        this.closeDrawer()
    },
    gotoDecoding() {
        wx.navigateTo({
            url: '/pages/index/sideBar/decoding/decoding',
        })
        this.closeDrawer()
    },
    gotoReset() {
        wx.navigateTo({
            url: '/pages/index/sideBar/reset/reset',
        })
        this.closeDrawer()
    },
    gotoManagement() {
        wx.navigateTo({
            url: '/pages/index/sideBar/management/management',
        })
        this.closeDrawer()
    },
    gotoInstruction() {
        wx.navigateTo({
            url: '/pages/index/sideBar/instruction/instruction',
        })
        this.closeDrawer()
    },
    gotoAbout() {
        wx.navigateTo({
            url: '/pages/index/sideBar/about/about',
        })
        this.closeDrawer()
    },


    /**
     * 弹出按钮点击事件
     */
    upModal: function (e) {
        // console.log(e.currentTarget.dataset.id)
        this.setData({
            showModal: true
        })
    },
    /**
     * 隐藏
     */
    downModal: function () {
        this.setData({
            showModal: false
        });
    },
    //跳转到添加色温灯具页面
    gotoAddDevice(e) {
        let ID = e.currentTarget.dataset.id
        let index = 'index=' + app.globalData.roomIndex + '&&'
        // console.log(ID);
        let imgSrc = 'aDImg=/image/icon/device_cct.png&&'
        let type = 'type=' + e.currentTarget.dataset.devType + '&&'
        switch (ID) {
            case "01":
                wx.navigateTo({
                    url: '/pages/index/add/addDevice/addDevice?' + imgSrc + index + type,
                })
                this.setData({
                    showModal: false
                });
                break;
            case "02":
                this.sorry();
                // wx.navigateTo({
                //     url: '/pages/index/add/addDevice/addDevice?aDImg=/image/icon/device_rgb.png',
                // })
                break;
            case "03":
                this.sorry();
                // wx.navigateTo({
                //     url: '/pages/index/add/addDevice/addDevice?aDImg=/image/icon/brightness_dim.png',
                // })
                break;
            case "04":
                this.sorry();
                // wx.navigateTo({
                //     url: '/pages/index/add/addDevice/addDevice?aDImg=/image/icon/switch.png',
                // })
                break;
        }

    },

    /**
     * 功能建设中
     */
    sorry() {
        wx.showToast({
            title: '功能建设中！',
            duration: 3000,
        })
    }


})